package demo.practice.leetcode_base;

public class P19 {


    public ListNode removeNthFromEnd(ListNode head, int n) {
        int count = count(head);
        int k= count-n+1;
        if(k==1)
            return head.next;
        ListNode current= head;
        //
        for(int i=1;i<k-1;i++){
            current= current.next;
        }
        current.next=current.next.next;
        return head;
    }


    private  int count(ListNode node){
        int c=0;
        ListNode current= node;
        while (current!=null ){
            c++;
            current=current.next;
        }
        return c;
    }
}
